package com.zline.foodraise.listener;

import android.content.Context;
import android.view.GestureDetector;
import android.view.MotionEvent;

import com.zline.foodraise.entity.TemplateBean;
import com.zline.foodraise.util.DeviceUtil;
import com.zline.foodraise.view.PosterTemplateView;

/**
 * Created by heshantao on 15/10/26.
 * View  拖动监听器
 */
public class GestureListener implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener {
    private static final String LOG_TAG=GestureListener.class.getSimpleName();
    PosterTemplateView view;
    TemplateBean mtemplateBean;
    Context cxt;
    float leftTopX;
    float leftTopY;
    float rightBottomX;
    float rightBottomY;
    float density;
    float scale;

    public GestureListener(Context context,PosterTemplateView view,TemplateBean template){
        this.view=view;
        this.mtemplateBean=template;
        cxt=context;
        scale=view.templateScale;
        initTemplateData();
    }

    void initTemplateData(){
        density= DeviceUtil.getDeviceDesity(cxt);
        leftTopX=mtemplateBean.getAndroidImgX()*scale;
        leftTopY=mtemplateBean.getAndroidImgY()*scale;
        rightBottomX=(mtemplateBean.getAndroidImgX()+mtemplateBean.getAndroidImgWidth())*scale;
        rightBottomY=(mtemplateBean.getAndroidImgY()+mtemplateBean.getAndroidImgHeight())*scale;
    }

    @Override
    public boolean onSingleTapConfirmed(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDoubleTap(MotionEvent e) {
        //view.onResetLocation();
        return true;
    }

    @Override
    public boolean onDoubleTapEvent(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onDown(MotionEvent e) {
        float downX=e.getX();
        float downY=e.getY();
        if(downX>leftTopX&&downX<rightBottomX&&downY>leftTopY&&downY<rightBottomY){
            return true;
        }
        return false;
    }

    @Override
    public void onShowPress(MotionEvent e) {

    }

    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        view.onMove(-distanceX, -distanceY);
        return true;
    }

    @Override
    public void onLongPress(MotionEvent e) {

    }

    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        final float distanceTimeFactor = 0.4f;
        final float totalDx = (distanceTimeFactor * velocityX / 2);
        final float totalDy = (distanceTimeFactor * velocityY / 2);
        view.onAnimateMove(totalDx, totalDy,
                (long) (1000 * distanceTimeFactor));
        return true;
    }
}
